package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.Supplier;
import com.xy.model.TextBook;
import com.xy.service.SupplierService;
import com.xy.service.impl.SupplierServiceImpl;

/**
 * Servlet implementation class supplierServlet
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/supplierServlet" })
public class SupplierServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SupplierServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		SupplierService service = new SupplierServiceImpl();
		
//		添加供应商
		if ("add".equals(request.getParameter("status"))) {
		  
		  Supplier supplier = new Supplier();
		  supplier.setName(request.getParameter("name"));
		  supplier.setTel(request.getParameter("tel"));
		  supplier.setEmail(request.getParameter("email"));
		  supplier.setContactMan(request.getParameter("contactMan"));
		  supplier.setAddress(request.getParameter("address"));
		  supplier.setRemark(request.getParameter("remark"));
		  supplier.setCreateDate(new Date());
		  
		  if(service.add(supplier))
		    request.getRequestDispatcher("/supplierServlet?status=list&currentPage=0").forward(request,
	            response);
		  else {
		    System.out.println("error");
		    request.getRequestDispatcher("/pages/book/book002/supplier_add.jsp").forward(request,
                response);
		  }
		  
//		  查询所有供应商
		} else if ("list".equals(request.getParameter("status"))) {
		  int currentPage = 0;
	      if (request.getParameter("goalpage") == null) {
	        currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
	      } else
	        currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

	      PageResults pageResults = service.listByPage(currentPage);
	      
	      request.setAttribute("pageResults", pageResults);
	      request.setAttribute("status", "list");
	      request.setAttribute("list", null);

	      request.getRequestDispatcher("/pages/book/book002/supplier_list.jsp").forward(request, response);
		  
		  
//		  条件查询供应商
		} else if ("query".equals(request.getParameter("status"))) {
		  int currentPage = 0;
	      if (request.getParameter("goalpage") == null) {
	        currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
	      } else
	        currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;
	      
	      String name = null;
	      String list = null;
	      
	      if (request.getParameter("list") == null) {
	        name = request.getParameter("name") == null ? "%" : request.getParameter("name");
	        list = name;
	      } else {
	        list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
	        name = list;
	      }

	      PageResults pageResults = service.query(name, currentPage);

	      request.setAttribute("pageResults", pageResults);
	      request.setAttribute("status", "query");
	      request.setAttribute("list", list);

	      request.getRequestDispatcher("/pages/book/book002/supplier_list.jsp").forward(request, response);
	      
	      
//		  删除供应商
		} else if ("del".equals(request.getParameter("status"))) {
		  int id = Integer.valueOf(request.getParameter("id"));
		  String preStatus = request.getParameter("preStatus");
		  int currentPage = 0;
		  if (request.getParameter("currentPage") != null) {
            currentPage = Integer.valueOf(request.getParameter("currentPage"));
		  }
		  
		  Supplier supplier = new Supplier();
          supplier.setId(id);
		  
		  if("query".equals(preStatus)) {
		    String list = null;
		    if (request.getParameter("list") == null) {
		      list = null;
		    }else
		      list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
		    
		    request.setAttribute("list", list);
		    if (service.delete(supplier)) {
	            request.getRequestDispatcher("/supplierServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
	                response);
	          } else {
	            System.out.println("error");
	            request.getRequestDispatcher("/supplierServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
	                response);
	          }
		  }else {
		    if (service.delete(supplier)) {
	            request.getRequestDispatcher("/supplierServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
	                response);
	          } else {
	            System.out.println("error");
	            request.getRequestDispatcher("/supplierServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
	                response);
	          }
		  }
		  
	      
//		  更新供应商
		} else if ("up".equals(request.getParameter("status"))) {
		  int id = Integer.valueOf(request.getParameter("id"));
		  Supplier supplier = service.get(id);
		  supplier.setName(request.getParameter("name"));
          supplier.setTel(request.getParameter("tel"));
          supplier.setEmail(request.getParameter("email"));
          supplier.setContactMan(request.getParameter("contactMan"));
          supplier.setAddress(request.getParameter("address"));
          supplier.setRemark(request.getParameter("remark"));
          
          if(service.update(supplier)) {
            request.getRequestDispatcher("/supplierServlet?status=list&currentPage=0").forward(request,
                response);
          }else {
            System.out.println("error");
            request.getRequestDispatcher("/supplierServlet?status=list&currentPage=0").forward(request,
                response);
          }
          
		  
//		   查看供应商详情
		} else if ("detail".equals(request.getParameter("status"))) {
		  int id = Integer.valueOf(request.getParameter("id"));
          Supplier supplier = service.get(id);
	      request.setAttribute("supplier", supplier);
	      if("up".equals(request.getParameter("pre")))
	        request.getRequestDispatcher("/pages/book/book002/supplier_modify.jsp?").forward(request,
	            response);
	      else
	        request.getRequestDispatcher("/pages/book/book002/supplier_detail.jsp").forward(request,
	            response);
		}
	}

}
